package com.vben.auth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.vben.auth.service.LoginService;
import com.vben.common.core.util.R;
import com.vben.system.api.dto.user.SysUserLoginDTO;
import com.vben.system.api.vo.user.SysUserLoginVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登陆相关操作
 *
 * @author sunnsun
 */
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    @Resource
    private HttpServletRequest request;

    /**
     * 账号密码登陆
     */
    @PostMapping("/login")
    public R<SysUserLoginVO> login(@RequestBody SysUserLoginDTO loginInfo) {
        return R.ok(loginService.login(loginInfo,request));
    }

    /**
     * 手机验证码登陆
     */
    @PostMapping("/login/phone")
    public R<SysUserLoginVO> loginByPhone(@RequestBody SysUserLoginDTO loginInfo) {
        return R.ok(loginService.loginByPhone(loginInfo,request));
    }

    /**
     * 注销登录
     */
    @PostMapping("/logout")
    @SaCheckLogin
    public R<SysUserLoginVO> logout() {
        loginService.logout();
        return R.ok();
    }

}
